﻿@page "/"
@using MyToDoList.Data
@inject ToDoListService _todoSvc
@inject NavigationManager NavigationManager

<MAppBar App>
    <MToolbarTitle>我的代办列表</MToolbarTitle>
    <MSpacer></MSpacer>
    <NavLink href="/edit">
        <MButton icon>
            <MIcon>mdi-bell-plus</MIcon>
        </MButton>
    </NavLink>
    <NavLink href="/settings/index">
        <MButton icon>
            <MIcon>mdi-cog</MIcon>
        </MButton>
    </NavLink>
</MAppBar>

<!-- 根据应用组件来调整你的内容 -->
<MMain>
    <!-- 给应用提供合适的间距 -->
    <MContainer Fluid>
        @if (todos == null || todos.Count == 0)
        {
            <p style="text-align:center">
                <MIcon Style="font-size:3.75rem;">mdi-dropbox</MIcon>
            </p>
            <p class="text-h4" style="text-align:center;">空空如也...</p>
        }
        else
        {
            <MList TwoLine>
            <MListItemGroup Color="primary">
                @foreach(var todo in todos)
                    {
                        <MListItem @onclick="@(_=>OpenDetailAsync(todo))">
                            <MListItemContent>
                                <MListItemTitle>@todo.Content</MListItemTitle>
                            </MListItemContent>
                        </MListItem>
                    }
                </MListItemGroup>
        </MList>
        }
    </MContainer>
</MMain>

@code {
    List<ToDoModel> todos = new();

    protected override async Task OnInitializedAsync()
    {
        todos = await _todoSvc.GetAllAsync();
    }

    private async Task OpenDetailAsync(ToDoModel todo)
    {
        NavigationManager.NavigateTo($"/edit?todoId={todo.Id.ToString()}");
    }
}
